<?php 

class QuestionController extends Controller{
	public $db;
	public function __init()
	{
		$this->db=K('Question');
		//问题类别
		$cat=K('Category')->getList();
		$this->assign('cat',$cat);
	}
	// 显示问题列表
	public function index()
	{
		$data=$this->db->getList();
		$this->assign($data);
		$this->display();
	}
	//发布问题
	public function add()
	{
		if (IS_POST) {
			if ($this->db->addQuestion()) {
				$this->success('发布成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$cat=K('Category')->getList();
			$this->assign('cat',$cat);
			$this->display();
		}
	}
	//编辑问题
	public function edit()
	{
		if (IS_POST) {
			if ($this->db->editQuestion()) {
				$this->success('修改成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$question =$this->db->find(Q('qid',0,'intval'));
			//缩略图处理(为了编辑时正常显示)
			//DEBUG
			$question['thumb']=array(
				array('path'=>$question['thumb'])
			);
			//END DEBUG
			$this->assign('question',$question);
			$cat=K('Category')->getList();
			$this->assign('cat',$cat);
			$this->display();
		}
	}
	//删除问题
	public function del()
	{
		$qid=Q('qid');
		if ($this->db->delQuestion($qid)) {
			$this->success('删除成功','index');
		}else{
			$this->error($this->db->error);
		}
	}


}

 ?>